package com.gzbsoft.sc.modules.sys.entity;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.gzbsoft.sc.common.config.Json2LongConverter;
import io.ebean.annotation.*;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.data.domain.Auditable;
import org.springframework.data.domain.Persistable;

import javax.persistence.Id;
import javax.persistence.MappedSuperclass;
import javax.persistence.Transient;
import java.time.LocalDateTime;
import java.util.Optional;
@Data
@MappedSuperclass
@AllArgsConstructor
@NoArgsConstructor
public abstract class BaseEntity implements Persistable<Long>, Auditable<String, Long, LocalDateTime> {
    @Id
    protected Long id;
    @WhoCreated
    String createdBy;
    @CreatedTimestamp
    LocalDateTime createdDate;
    @WhoModified
    String lastModifiedBy;
    @UpdatedTimestamp
    LocalDateTime lastModifiedDate;
    @SoftDelete
    private boolean delFlag;
    private String remarks;
    public BaseEntity(Long id) {
        super();
        this.id = id;
    }
    @Override
    @JsonSerialize(using = Json2LongConverter.class)
    public Optional<LocalDateTime> getCreatedDate() {
        return Optional.ofNullable(this.createdDate);
    }
    @Override
    @JsonSerialize(using = Json2LongConverter.class)
    public Optional<LocalDateTime> getLastModifiedDate() {
        return Optional.ofNullable(this.lastModifiedDate);
    }
    @Override
    public Optional<String> getCreatedBy() {
        return Optional.ofNullable(createdBy);
    }
    @Override
    public Optional<String> getLastModifiedBy() {
        return Optional.ofNullable(lastModifiedBy);
    }
    @Transient
    @Override
    public boolean isNew() {
        return null == getId();
    }
}
